package com.anysoft.formula.filter;

import com.anysoft.formula.DataProvider;
import com.anysoft.formula.DateUtil;
import com.anysoft.formula.Filter;

import java.util.Date;

/**
 * 时间戳格式化为字符串
 */
public class Timestamp extends Filter {

    @Override
    protected String doFilter(String data, String pattern, DataProvider provider){
        long now = getTimestamp(data,System.currentTimeMillis());
        return DateUtil.formatDate(new Date(now), pattern);
    }

    protected long getTimestamp(String data,long dft){
        try{
            return Long.parseLong(data);
        }catch (NumberFormatException ex){
            return dft;
        }
    }
}
